package com.common.strategy.impl;

import com.common.authentication.Oauth2PasswordModeAuthentication;
import com.common.model.SystemUserModel;
import com.common.mysql.Where;
import com.common.pojo.Oauth2PasswordMode;
import com.common.sevice.SystemUserService;
import com.common.strategy.AccountTypeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component("account")
public class PasswordAccountTypeStrategy implements AccountTypeStrategy<Oauth2PasswordModeAuthentication, Oauth2PasswordMode> {

    @Autowired
    private SystemUserService systemUserService;

    @Override
    public Oauth2PasswordModeAuthentication login(Oauth2PasswordMode mode) {
        //实现简单的登录认证逻辑
        SystemUserModel systemUserModel = systemUserService.searchOne(Where.build().field("account").equal(mode.getAccount())
                .and().field("password").equal(mode.getPassword())
                .and().field("del_flag").equal("0"));
        if (ObjectUtils.isEmpty(systemUserModel)){
            return null;
        }
        return new Oauth2PasswordModeAuthentication(systemUserModel.getId());
    }
}
